package emulator.hardware.bits;

public class EdgeDetector extends SharedBit {
	private boolean trigger_level;
	private boolean actual_level;

	public EdgeDetector(boolean trigger_level, boolean initial_value)
	{
		this.trigger_level = trigger_level;
		this.actual_level = initial_value;
		super.setValue(false);
	}

	/* (non-Javadoc)
	 * @see emulator.util.SharedBit#setValue(boolean)
	 */
	@Override
	public synchronized void setValue(boolean value) {
		if (actual_level != value)
		{
			actual_level = value;
			if (actual_level == trigger_level)
				super.setValue(true);
		}
	}

	/* (non-Javadoc)
	 * @see emulator.util.SharedBit#getValue()
	 */
	@Override
	public synchronized boolean getValue() {
		boolean return_value = super.getValue();
		super.setValue(false);
		return return_value;
	}

	/* (non-Javadoc)
	 * @see emulator.util.SharedBit#toggle()
	 */
	@Override
	public synchronized void toggle() {
		setValue(!actual_level);
	}
}
